package com.ht.medicaltreatment.activity.haocai

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.activity.HaoCaiProgressActivity
import com.ht.medicaltreatment.activity.ShenQingDetailActivity
import com.ht.medicaltreatment.adapter.HaoCaiShenQingAdapter
import com.ht.medicaltreatment.bean.HaoCaiShenQingListBean
import com.ht.medicaltreatment.viewmodel.HaoCaiShenQingViewModel
import kotlinx.android.synthetic.main.activity_haocaishenqing_layout.btnPositive
import kotlinx.android.synthetic.main.activity_haocaishenqing_layout.recyclerView
import kotlinx.android.synthetic.main.activity_haocaishenqing_layout.tvCheckCount
import kotlinx.android.synthetic.main.activity_haocaishenqing_layout.tvRight

/**
 * 耗材申请 列表
 */
class HaoCaiShenQingListActivity:BaseActivity(), OnItemClickListener {

    override fun getLayoutId() = R.layout.activity_haocaishenqing_layout

    private val adapter = HaoCaiShenQingAdapter()
    private val model by viewModels<HaoCaiShenQingViewModel>()
    private var page = 1
    private var projectId = 1

    override fun init() {
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        projectId =  intent.getIntExtra("projectId",-1)

        //申请
        btnPositive.setOnClickListener {
            val intent = Intent(this,ShenQingActivity::class.java)
            intent.putExtra("projectId","$projectId")
            startActivity(intent)
        }

        //使用进度
        tvRight.setOnClickListener {
            val intent = Intent(this,HaoCaiProgressActivity::class.java)
            intent.putExtra("projectId","$projectId")
            startActivity(intent)
        }

        initData()

    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    fun initData(){
        showProgress()
        model.getList("$page","$projectId"){
            dismissProgress()
            tvCheckCount.text = "${it.totalNumber}"
            adapter.setNewInstance(it.pageList)
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as HaoCaiShenQingListBean.DataBean.PageListBean
        val intent = Intent(this,ShenQingDetailActivity::class.java)
        intent.putExtra("id",item.id)
        startActivity(intent)
    }
}